<?php
/**
 * Created by PhpStorm.
 * User: lixy
 * Date: 2015/12/29
 * Time: 17:27
 */

namespace app\models\weixin;

use Yii;
use app\lib\BaseModel;
class T_WXMALL_USER extends BaseModel
{

    public static function tableName()
    {
        return 'T_WXMALL_USER';
    }
/*
wx_openid


nick_name
header_url
*/

    //添加用户 返回用户id
    public static function addUser($userinfo)
    {
       $user= static::getUserByOpenId($userinfo["WX_OPENID"]);
        if(count($user)==0){
            $userid= static::uuid();
            $command = static::query()->createCommand('insert into T_WXMALL_USER (ID,WX_OPENID,NICK_NAME,HEADER_URL,CREATE_DATETIME,DR) VALUES (:ID,:WX_OPENID,:NICK_NAME,:HEADER_URL,:CREATE_DATETIME,0)');
            $command->bindValue(':ID',$userid);
            $command->bindValue(':WX_OPENID', $userinfo["WX_OPENID"]);
            $command->bindValue(':NICK_NAME', $userinfo["NICK_NAME"]);
            $command->bindValue(':HEADER_URL', $userinfo["HEADER_URL"]);
            $command->bindValue(':CREATE_DATETIME',date("Y-m-d H:i:s",time()));
            if($command->execute() == 0){
                echo "写入用户失败！";
                die;
            }
            return $userid;
        }else{
            $user=$user[0];
            $userid= $user["ID"];
            if($user["NICK_NAME"]==$userinfo["NICK_NAME"] && $user["HEADER_URL"]==$userinfo["HEADER_URL"]){
                return $userid;
            }else{
                //更新信息 用户更新信息 ：创建时间不更新，只更新头像和昵称

//                $command = static::query()->createCommand('update T_WXMALL_USER set NICK_NAME=:NICK_NAME,HEADER_URL=:HEADER_URL,CREATE_DATETIME=:CREATE_DATETIME  where ID=:ID');
                $command = static::query()->createCommand('update T_WXMALL_USER set NICK_NAME=:NICK_NAME,HEADER_URL=:HEADER_URL  where ID=:ID');
                $command->bindValue(':ID',$userid);
                $command->bindValue(':NICK_NAME', $userinfo["NICK_NAME"]);
                $command->bindValue(':HEADER_URL', $userinfo["HEADER_URL"]);
//                $command->bindValue(':CREATE_DATETIME',date("Y-m-d H:i:s",time()));
                if($command->execute() == 0){
                    echo "写入用户失败！";
                    die;
                }
                return $userid;
            }


        }

    }
    //通过openid 查询用户id
    public static function getUserIdByOpenId($openid){
        $command = static::query()->createCommand('select ID from T_WXMALL_USER where WX_OPENID=:WX_OPENID');
        $command->bindValue(':WX_OPENID',$openid);


        return $command->queryScalar();
    }
    //通过openid 查询用户id
    public static function getUserCount(){
        $command = static::query()->createCommand('select count(1) from T_WXMALL_USER ');


        return $command->queryScalar();
    }
    //通过openid 查询用户
    public static function getUserByOpenId($openid){
        $command = static::query()->createCommand('select * from T_WXMALL_USER where WX_OPENID=:WX_OPENID');
        $command->bindValue(':WX_OPENID',$openid);


        return $command->queryAll();
    }
    //通过userid 查询用户
    public static function getUserByUserid($userid){
        $command = static::query()->createCommand('select * from T_WXMALL_USER where id=:userid');
        $command->bindValue(':userid',$userid);


        return $command->queryAll();
    }
    //绑定客户手机号
    public static function set_user_tel($userid,$tel){
        $cmd=  static::query()->createCommand('update t_wxmall_user set phone=:phone where id=:userid');
        $cmd->bindValue(':userid', $userid);
        $cmd->bindValue(':phone', $tel);
        return $cmd->execute();
    }
}